package es.us.lsi.aws.api.model;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

import com.google.appengine.api.datastore.Entity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Message extends BasicMessage {

	private String id;
	private Boolean sent;

	@SuppressWarnings("unchecked")
	public Message(Entity ent) {
		Gson gson = new Gson();
		this.setId((String) ent.getProperty("id"));
		this.setText((String) ent.getProperty("text"));
		this.setSent((Boolean) ent.getProperty("sent"));
		this.setLstSocialAccount((List<SocialAccount>) gson.fromJson(
				(String) ent.getProperty("lstSocialAccount"),
				new TypeToken<Collection<SocialAccount>>() {
				}.getType()));
		this.setDateToPublish((Long) ent.getProperty("dateToPublish"));
	}

	private Message(String id, String text, Boolean sent,
			List<SocialAccount> lstSocialAccount, Long dateToPublish) {
		super(text, lstSocialAccount,dateToPublish);
		this.id = id;
		this.sent = sent;
	}

	public Message(String text, List<SocialAccount> lstSocialAccount, Long dateToPublish) {
		this(UUID.randomUUID().toString(), text, false, lstSocialAccount,dateToPublish);
	}

	public Entity createEntity() {
		Entity ent = new Entity(this.getClass().getSimpleName());
		ent.setProperty("id", this.getId());
		ent.setProperty("text", this.getText());
		ent.setProperty("sent", this.getSent().booleanValue());
		Gson gson = new Gson();
		ent.setProperty("lstSocialAccount",
				gson.toJson(this.getLstSocialAccount()));
		ent.setProperty("dateToPublish",this.getDateToPublish());

		return ent;
	}

	public Boolean getSent() {
		return sent;
	}

	public void setSent(Boolean sent) {
		this.sent = sent;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
